//
//  TLMessageViewController.m
//  WXReader
//
//  Created by huaya on 2025/3/20.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLMessageViewController.h"

#import "TLMsgTableViewCell.h"
#import "TLMsgModel.h"

@interface TLMessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSNumber *pageNum;
@property (nonatomic, strong) NSNumber *pageSize;
@property (nonatomic, strong) NSMutableArray<TLMsgModel *> *dataArr;
@end

@implementation TLMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr = [NSMutableArray new];
    self.pageNum = [NSNumber numberWithInt:1];
    self.pageSize = [NSNumber numberWithInt:20];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
    [self.tableView.mj_header beginRefreshing];
}
- (UITableView *)tableView{
    if (!_tableView) {
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, MK_SCREEN_SAVE_BOTTOM_HEIGHT)];
        footView.backgroundColor = UIColor.clearColor;
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"F8F8FA"];
        _tableView.tableFooterView = footView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        if (@available(iOS 15.0, *)) {
            _tableView.sectionHeaderTopPadding = 0;
        }
        
        [_tableView registerNib:[UINib nibWithNibName:@"TLMsgTableViewCell" bundle:nil] forCellReuseIdentifier:@"TLMsgTableViewCell"];
        
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadLastData)];
        header.lastUpdatedTimeLabel.hidden = YES;
        _tableView.mj_header = header;

        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        
    }
    return _tableView;
}
- (void)resetFrame:(CGRect)frame{
    NSLog(@"%@",NSStringFromCGRect(frame));
    self.view.frame = frame;
    self.tableView.frame = self.view.bounds;
}
- (void)loadLastData{
    
    self.pageNum = [NSNumber numberWithInt:1];
    
    [self network];
}
- (void)loadMoreData{
    
    self.pageNum = [NSNumber numberWithInt:(self.pageNum.intValue+1)];
    
    [self network];
}
- (void)network{
    
    NSDictionary *param = @{
        @"pageNum":self.pageNum,
        @"pageSize":self.pageSize,
    };
    
    [APIManager sendGetRequest:[APIURLManager systemNoticeList] parameters:param model:TL_UserInfoModel.class success:^(BOOL success, id  _Nullable responseData, APIResponse * _Nonnull requestModel) {
        
        if (success) {
            NSLog(@"%@",requestModel);
            
            NSArray *data = [TLMsgModel mj_objectArrayWithKeyValuesArray:requestModel.data[@"records"]];
            
            [self.dataArr addObjectsFromArray:data];
            
            [self.tableView.mj_header endRefreshing];
            if (data.count<self.pageSize.intValue) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
            
        }
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }];
}

// 添加自动布局调整
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}

#pragma mark - table delegate - datasource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TLMsgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TLMsgTableViewCell"];

    TLMsgModel *model = self.dataArr[indexPath.row];
    
    cell.model = model;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 375;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TLMsgModel *model = self.dataArr[indexPath.row];
    
    if (![model.readStatus isEqualToString:@"1"]) {
        
        [APIManager sendGetRequest:[APIURLManager systemNoticeWithNoticeId:model.noticeId] parameters:nil model:nil success:^(BOOL success, id  _Nullable responseData, APIResponse * _Nonnull requestModel) {
            if (success) {
                model.readStatus = @"1";
                [self.tableView reloadData];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
        }];
        
    }
    
    if ([model.noticeType isEqualToString:@"5"]) {
        [TL_JumpManager jumpActionWithType:@"/rechargevip" content:@"" otherParameter:@{} completion:nil];
    }
    
    if ([model.noticeType isEqualToString:@"4"]) {
        [TL_JumpManager jumpActionWithType:Jump_internalWebview content:model.noticeLink otherParameter:@{} completion:nil];
    }
    
    if ([model.noticeType isEqualToString:@"3"]) {
        [TL_JumpManager jumpActionWithType:@"/recharge" content:@"" otherParameter:@{} completion:nil];
    }
    
    if ([model.noticeType isEqualToString:@"2"]) {
        //短篇
        TLShortStoryViewController *vc = [TLShortStoryViewController new];
        vc.worksId = model.worksId.stringValue;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if ([model.noticeType isEqualToString:@"1"]){
        //长篇
//        TL_BookMallDetailViewController *vc = [TL_BookMallDetailViewController new];
//        vc.book_id = [model.worksId intValue];
//        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
@end
